package com.bac.rentmap.ad.publisher;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Information about ad publishers. Stores how many ads are published by some
 * person.
 *
 * @author Andrew Osipenko
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
class AdPublisher {

    @SuppressWarnings("unused")
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String phone;

    @Persistent
    private int adCount;

    @SuppressWarnings("unused")
    private Date lastPublishingDate;

    AdPublisher(String phone, int adCount) {
        this.key = generateKey(phone);
        this.phone = phone;
        setAdCount(adCount);
    }

    public String getPhone() {
        return phone;
    }

    public int getAdCount() {
        return adCount;
    }

    public void setAdCount(int adCount) {
        this.adCount = adCount;
        this.lastPublishingDate = new Date();
    }

    public static Key generateKey(String phone) {
        return KeyFactory.createKey(AdPublisher.class.getSimpleName(), "p"
                + phone);
    }
}
